<?php
/**
 * 用户目录控制器
 **/
class CategoriesController extends AppController{
	var $name='Categories';
	var $uses=array('BlogsCategory','Follow','Tag','Userinfo');
	var $autoRender = false;
	var $autoLayout = false;

	/** 
		* @brief create_a_view 创建用户导航栏
		* 返回包含导航栏中所需信息的数组
		* 
		* @return 
	 */
	function create_a_view(){
		$user_id = $this->Session->read('User.user_id');
		$Categorys=array();
		$Categorys['BlogsCategory']=$this->BlogsCategory->findAllByUser_id($user_id);
		$Categorys['Follow']=$this->Follow->findAllByUser_id($user_id);
		$Categorys['Tag']=$this->Tag->findAllByUser_id($user_id);
		// print_r($user_id);
		// print_r($Categorys);
		return $Categorys;
	}

	/**
	 * show_navigation 
	 * 返回导航栏视图
	 * @access public
	 * @return void
	 */
	function show_navigation(){
		$this->render('/elements/mainNavigation');
	}
	/** 
		* @brief add_blog_folder 创建博客分类文件夹
		* 
		* 传入 $_POST['name'] 要创建的分类名
		* 失败返回 0；
		* 成功返回刚才添加的分类的ID
	 */
	function add_blog_folder(){
		if(isset($_POST['name']) && !empty($_POST['name'])){
			$folder_name=$_POST['name'];
			$this->BlogsCategory->save(
			array(
				'BlogsCategory' =>array(
					'user_id'=>$this->Session->read('User.user_id'),
					'name'=>$folder_name
					)
				)
			);
			$rs = $this->BlogsCategory->query("select @@IDENTITY");
			echo($rs[0][0]['@@IDENTITY']);
		}
		else{
			echo '0';
		}
	}

	/** 
		* @brief add_follow  添加关注人 
		*   用GET方法把参数要关注的人的id传入,变量名为follow_id
		* @return 1成功，0失败
	 */
	function add_follow(){
		if(isset($_GET['follow_id'])){
			$follow_id = $_GET['follow_id'];
			if($this->Follow->save(
				array(
					'Follow' =>array(
						'user_id' =>$this->Session->read('User.user_id'),
						'follow_id' => $follow_id
					)
				)
			))
			{
				echo '1';
			}
			else{
				echo '0';
				$this->render('/elements/sql_dump');
			}
		}
	}

	
	/**
	 * display_tag
	 * 显示推荐关注的标签
	 *    无新标签时返回0(暂时无此功能)
	 * @access public
	 * @return void
	 */
	
	function display_tag(){
		$this->autoRender = true;
		$this->paginate = array(
			'Tag' =>array(
				'fields' => array('DISTINCT Tag.tag'),
				'limit' => 6,
				'order' => array('Tag.id' => 'asc')
				)
			);
		$data_tag = $this->paginate('Tag');
		if(count($data_tag) === 0){
			echo 0;
			$this->autoRender = false;
		}else{
			$this->set('tags', $data_tag);
		}
		//$this->render('/elements/sql_dump');
	}
	/**
	 * display_follow 
	 * 显示推荐关注用户
	 *    无新用户返回0(暂时此功能)
	 * @access public
	 * @return void
	 */
	function display_follow(){
		// $this->autoLayout = true;
		$this->autoRender = true;
		$this->paginate = array(
			'Userinfo'=>array(
				'fields' => array('Userinfo.id','Userinfo.nickname','Userinfo.avatar'),
				'limit' => 5,
				'order'=> array('Userinfo.id'=>'asc')
			)
		);
		$data_user = $this->paginate('Userinfo');
		// var_dump($data_user);
		// if(count($data_user) === 0){
			// echo 0;
			// $this->autoRender = false;
		// }else{
			$this->set('users', $data_user);
		// }
		// $this->render('/elements/sql_dump');
	}
	/**
	 * 添加标签
		*   用GET方法把要关注tag传入,变量名为tag
	 * 返回值：1成功，0失败。
	 * @access public
	 * @return void
	 */
	
	function add_tag(){
		if(isset($_GET['tag'])){
			// $tag = iconv('gbk','utf-8//IGNORE',$_GET['tag']);
			$tag = $_GET['tag'];
			// echo $tag;
			if($this->Tag->save(
				array(
					'Tag' =>array(
						'user_id' =>$this->Session->read('User.user_id'),
						'tag' => $tag,
					)
				)
			))
			{
				echo '1';
				return;
			}
		}
		echo '0';
	}
	/** 
		* @brief add_tag_folder 创建标签分类文件夹
		* 
		* @param $folder_name
		* 
		* @return 
	 */
	// function add_tag_folder($folder_name){
		// $this->TagsCategory->save(
			// array(
				// 'TagsCategory' => array(
					// 'user_id' => $user_id,
					// 'name' => $folder_name
				// )
			// )
		// );
	// }

	/** 
		* @brief add_tag_to_folder 添加标签到分类文件夹
		* 
		* @param $tag
		* @param $folder_id
		* 
		* @return 
	 */
	 //function add_tag_to_folder($tag,$folder_id){
		// $this->TagsCategory->updateAll(
			// array('TagsCategory.tags' => 'TagsCategory.tags + '.','.$tag),
			// array('TagsCategory.id' => $folder_id)
		// );
	// }
}
?>
